


project , original("commons_candidate_campaign_spending_v20201027.dta")



	*create data for candidate-level analyses
	use "commons_candidate_campaign_spending_v20201027.dta", clear //open data
	gen data_issue = notes_spending!="" //| notes_spending_limit!=""
	**drop observations: by-elections, ireland
	drop if nation=="Ireland":nation
	drop if by_election==1
	replace party= "Liberal (Dem.)" if party=="National Liberal"

	*lost deposits
	gen lost_deposits = 0 if year<1918
	replace lost_deposits = 1 if year>=1918 & year<=1985 & vote_pct<12.5
	replace lost_deposits = 1 if year>1986 & vote_pct<5	
	
	sort cand_id date
	gen lost_deposits_prev = 0
	bys cand_id: replace lost_deposits_prev = 1 if lost_deposits[_n-1]==1

	*replace vote/spending pct with 100 if the election is uncontested in single-member constituency
	replace vote_pct = 100/seats_up if candidates==seats_up // set vote share to 1 in uncontested seats
	replace vote_pct = 100 if obs_id==18950700454 
	replace spending_pct = 100/seats_up if  candidates==seats_up & spending_pct==. // set spending share to 1 in uncontested seats	
	
	replace spending_limit_predicted_adj = spending_limit_predicted_adj/10000
	
	*generate party dummies and interactions with spending limit
	gen con = party=="Conservative"
	gen lab = party=="Labour"
	gen labXlimit = lab*spending_limit_predicted_adj
	gen lib = party=="Liberal (Dem.)"	
	gen libXlimit = lib*spending_limit_predicted_adj
	gen other = inlist(party, "Liberal (Dem.)","Labour","Conservative")==0
	gen otherXlimit = other*spending_limit_predicted_adj		
	gen incXspending_limit_predicted_adj = inc*spending_limit_predicted_adj
	
	egen const_id = group(const)
	egen party_election = group(party date)	
	
	*label variables
	label var labXlimit "Spending Limit (\pounds10,000) $\times$ Labour"
	label var libXlimit "Spending Limit (\pounds10,000) $\times$ Liberal (Dem.)"
	label var otherXlimit "Spending Limit (\pounds10,000) $\times$ Other"
	label var incXspending_limit_predicted_adj "Incumbent \(\times\) Spending Limit (\pounds10,000)"
	label var inc "Incumbent"	
	label var spending_limit_predicted_adj "Spending Limit (\pounds10,000)"	
	
	save candidate_elections.dta, replace
	
	
	

	gen vtsh_sq = (vote_pct/100)^2 // calculate the square of vtsh	
	gen spsh_sq = (spending_pct/100)^2 // calculate the square of spsh
	
	*incumbents vote/spending shares
	gen inc_vote_pct = vote_pct if inc==1
	gen inc_spending_pct = spending_pct if inc==1

	sort const date vote_pct
	bys const date: egen lastwinner_vote_pct = min(vote_pct) if win==1
	bys const date: egen firstloser_vote_pct = max(vote_pct) if win==0

	
	gen major_vote_pct =vote_pct if inlist(party, "Liberal (Dem.)","Labour","Conservative")==1

	bys const: egen topchallenger_vote_pct = max(vote_pct) if inc==0
	bys const: egen topchallenger_spending_pct = max(spending_pct) if inc==0

	*collapse dataset such that each observation is a constituency-election observation
	collapse  (max) inc data_issue (sum) topchallenger_*_pct major_vote_pct lost_deposits inc_vote_pct inc_spending_pct vtsh_sq spsh_sq (mean)  lastwinner_vote_pct firstloser_vote_pct spending_*_adj  year county candidates  electors spending_filed  seats_up seats_total (first) const_id region nation const_alternative ,by(const date)
	
	replace firstloser_vote_pct = 0 if candidates==seats_up // the vote share of the first loser does not exist for uncontested elections
	gen margin = lastwinner_vote_pct - firstloser_vote_pct
	sort const date
	bys const: gen margin_prev = margin[_n-1]
	gen competitive_prev = abs(margin_prev)<10 if margin_prev!=.
	gen limitXcompetitive = competitive_prev*spending_limit_predicted_adj
	gen limitXmargin_prev = margin_prev*spending_limit_predicted_adj
	
	bys const: gen lost_deposit_prev = lost_deposit[_n-1]
	
	replace inc_vote_pct = . if inc==0
	replace inc_spending_pct = . if inc==0
	replace topchallenger_vote_pct = . if inc==0
	replace topchallenger_spending_pct = . if inc==0
	
	*replace spending variables with missing if spending is not reported
	foreach v of varlist  inc_spending_pct spending_*_adj {
		replace  `v'=. if spending_filed>=0 & spending_filed<1
	}

	

	gen countyXelectors = county*electors
	gen effective_candidates = 1/vtsh_sq
	gen effective_spenders = 1/spsh_sq

	egen election = group(date)
	xtset const_id election
		
	label var topchallenger_vote_pct "Top Challenger Vote %"
	label var topchallenger_spending_pct "Top Challenger Spending %"
	label var spending_limit_predicted_adj "Spending Limit (\pounds10,000)"
	label var spending_total_adj "Spending"
	label var candidates "Candidates"
	label var effective_candidates "Effective Candidates"
	label var effective_spenders "Effective Spenders"
	label var inc_vote_pct "Incumbent Vote %"
	label var inc_spending_pct "Incumbent Spending %"	
	label var competitive_prev "Competitive, t-1"
	label var margin_prev "Margin of Victory, t-1"
	label var limitXcompetitive "Spending Limit (\pounds10,000)\\ $\times$ Competitive, t-1"	
	label var limitXmargin_prev "Spending Limit (\pounds10,000)\\ $\times$ Margin of Victory, t-1"	

	save "constituency_elections.dta", replace
	
	
	contract const date competitive_prev margin_prev
	drop _freq
	merge 1:m const date using "candidate_elections.dta"
	drop _merge
	save "candidate_elections.dta", replace
	
	
	project , creates("candidate_elections.dta")		
	project , creates("constituency_elections.dta")	
	
	
